package com.emmy.loveview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class LoveView extends View {

    private Paint myPaint;
    private Path path;

    private boolean isAntimateOn = true;
    private final int DEFAULT_COLOR = Color.RED;

    private int color = DEFAULT_COLOR;

    private final float DEFAULT_MIN_RATE = 5.0f;
    private final float DEFAULT_MAX_RATE = 8.0f;
    private final float DEFAULT_STEP_RATE = 0.05f;

    private float max_rate = DEFAULT_MAX_RATE;
    private float min_rate = DEFAULT_MIN_RATE;
    private float step_rate = DEFAULT_STEP_RATE;


    //半径变化率
    private float rate = DEFAULT_MIN_RATE;

    private boolean orderByDesc = false;

    private View.OnClickListener onClickListener = null;
    private AnimateThread animateThread;

    public LoveView(Context context){
        super(context);
    }

    public LoveView(Context context, AttributeSet attributes){
        this(context, attributes, 0);
    }

    public LoveView(Context context, AttributeSet attributes, int defStyle){
        super(context, attributes, defStyle);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attributes,
                R.styleable.LoveView,defStyle,0);
        typedArray.recycle();
        init();
    }

    public void setAntimateOn(boolean antimateOn) {
        isAntimateOn = antimateOn;
    }

    public boolean getAntimateOn(){
        return isAntimateOn;
    }

    public void setColor(int color) {
        this.color = color;
        invalidate();
    }

    public int getColor() {
        return color;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //重置画板
        path.reset();
        myPaint.setColor(color);

        //得到中点坐标
        int px = getMeasuredWidth()/2;
        int py = getMeasuredHeight()/2;

        //根据心形函数画心
        for (double i = 0;i<2*Math.PI;i+=0.001){
            float x = (float)(16 * Math.sin(i) * Math.sin(i) * Math.sin(i));
            float y = (float)(13 * Math.cos(i) - 5 * Math.cos(2 * i) - 2 * Math.cos(3 * i) - Math.cos(4 * i));
            x *= rate;
            y *= rate;
            x = px - x;
            y = py - y;
            path.lineTo(x, y);
        }
        canvas.drawPath(path, myPaint);
    }

    //初始化画笔
    private void init() {
        myPaint = new Paint();
        myPaint.setAntiAlias(true);
        //画笔style为填充
        myPaint.setStyle(Paint.Style.FILL);
        myPaint.setStrokeWidth(2);
        //初始化路径
        path = new Path();
        if (isAntimateOn){
            animateThread = new AnimateThread();
            animateThread.start();
        }
    }

    private class AnimateThread extends Thread{
        public void run(){
            while (true) {
                if (!orderByDesc){
                    rate += DEFAULT_STEP_RATE;
                    if (rate>DEFAULT_MAX_RATE)
                        orderByDesc = true;
                }else{
                    rate -= DEFAULT_STEP_RATE;
                    if (rate<DEFAULT_MIN_RATE)
                        orderByDesc = false;
                }
                try {
                    Thread.sleep(50);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                postInvalidate();
            }
        }
    }

    @Override
    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (onClickListener!=null){
            this.onClickListener.onClick(this);
        }
        return super.onTouchEvent(event);
    }

    public void setMax_rate(float max_rate) {
        this.max_rate = max_rate;
    }

    public void setMin_rate(float min_rate) {
        this.min_rate = min_rate;
    }

    public void setStep_rate(float step_rate) {
        this.step_rate = step_rate;
    }
}
